﻿using System.Windows;
using Baosight.ZNH.ZMCommon;
using System.Collections.Generic;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Telerik.Windows;

namespace Baosight.ZNH.ZMCC.UI
{
    /// <summary>
    /// 质量标准化项目管理  
    /// </summary>
    /// Title:ZMCC00
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 严洪浩
    /// Version: 1.0
    /// History:
    ///       2011-06-27  严洪浩   [创建]
    ///
    public partial class ZMCC00 : EFViewCmes
    {
        private EFTreeItem item;//记录选中的树节点

        public ZMCC00()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCC00_Loaded);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCC00_Loaded(object sender, RoutedEventArgs e)
        {
            this.qualityManagementTree.Refresh();
        }

        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void qualityManagementTree_Selected(object sender, RadRoutedEventArgs e)
        {
            item = this.qualityManagementTree.SelectedItems[0] as EFTreeItem;
            string level = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;//树的级别

            Dictionary<string, string> args = this.GetTreeNodeInfo(item);
            if (level.Equals("0"))
            {
                //点击0级节点 分项
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCC01", args);
            }
            else if (level.Equals("1"))
            {
                //点击1级节点 分类
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCC02", args);
            }
            else if (level.Equals("2"))
            {
                //点击2级别节点 分类下级
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCC03", args);
            }
            else
            {
                //叶子节点 下级明细
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCC04", args);
            }
        }

        
        /// <summary>
        /// //获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetTreeNodeInfo(EFTreeItem item)
        {
            string fdKeyId = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;//主键
            string level = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;//树的级别
            string isLeaf = (item.IsLeaf == true) ? "1" : "0";

            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("fdParentId", fdKeyId);
            args.Add("isLeaf", isLeaf);
            args.Add("level", level);

            return args;
        }

        /// <summary>
        /// 重新基类的发布数据方法
        /// 刷新树节点
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(EIInfo data)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string _value = "";
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("value").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = (item.Parent as EFTreeItem).ItemObject.GetType().GetProperty("level").GetValue((item.Parent as EFTreeItem).ItemObject, null) as string;
                    args.Add("level", _value);
                    _value = ((item.Parent as EFTreeItem).IsLeaf == true) ? "1" : "0";//是否叶子节点
                    args.Add("isLeaf", _value);
                }
                else
                {
                    _value = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;
                    args.Add("node", _value);
                    _value = item.ItemObject.GetType().GetProperty("level").GetValue(item.ItemObject, null) as string;
                    args.Add("level", _value);
                    args.Add("isLeaf", "0");
                }
            }

            BaseTools.RefreshTree("ZMCC00", "query", args, OnDoWorkCompleted);
        }

        /// <summary>
        /// OnEIDataPublished回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            this.qualityManagementTree.EIDataContext = new Platform.Core.UI.EIDataContext();
            this.qualityManagementTree.EIDataContext.Update(e.EIInfo);
            item.IsLoadOnDemandEnabled = true;
            if (item.IsLeaf)
            {
                string node = item.ItemObject.GetType().GetProperty("value").GetValue(item.ItemObject, null) as string;

                EFTreeItem parent = item.Parent as EFTreeItem;
                parent.Items.Clear();
                parent.CollapseItem();
                parent.ExpandItem();

                item = BaseTools.FindTreeItem(parent.Items, node, "value") as EFTreeItem;
                this.qualityManagementTree.SelectedItem = item;
                return;
            }
            item.Items.Clear();
            item.CollapseItem();
            item.ExpandItem();
        }

        /// <summary>
        /// 第一次进入页面时选中树的根节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void qualityManagementTree_ItemPrepared(object sender, RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            if (treeItem.Level == 0)
            {
                this.qualityManagementTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }

    }
}
